using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using DAO;
using System.Data;

namespace BUS
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class CONGNOControl
    {
        #region Local Variable
        private CONGNODataAccess _objDAO;
        #endregion Local Variable

        #region Method
        public CONGNOControl()
        {
            _objDAO = new CONGNODataAccess();
        }

        public CONGNOInfo Get(
        Int64 ID,
        ref string sErr)
        {
            return _objDAO.Get(
            ID,
            ref sErr);
        }

        public DataTable GetAll(
        ref string sErr)
        {
            return _objDAO.GetAll(
            ref sErr);
        }
        public DataTable GetByPos(
        int pos, ref string sErr)
        {
            return _objDAO.GetByPos(
            pos, ref sErr);
        }
        public int GetCountRecord(
        ref string sErr)
        {
            return _objDAO.GetCountRecord(
            ref sErr);
        }

        public Int32 Add(CONGNOInfo obj, ref string sErr)
        {
            return _objDAO.Add(obj, ref sErr);
        }

        public string Update(CONGNOInfo obj)
        {
            return _objDAO.Update(obj);
        }

        public string Delete(
        Int64 ID
        )
        {
            return _objDAO.Delete(
            ID
            );
        }
        public Boolean IsExist(
        Int64 ID
        )
        {
            return _objDAO.IsExist(
            ID
            );
        }

        public DataTableCollection Get_Page(CONGNOInfo obj, string orderBy, int pageIndex, int pageSize, ref String sErr)
        {
            return _objDAO.Get_Page(obj, orderBy, pageIndex, pageSize, ref sErr);
        }

        public DataTable Search(String columnName, String columnValue, String condition, String tableName, ref String sErr)
        {
            return _objDAO.Search(columnName, columnValue, condition, tableName, ref  sErr);
        }
        public string InsertUpdate(CONGNOInfo obj)
        {
            string sErr = "";
            if (IsExist(
            obj.ID
            ))
            {
                sErr = Update(obj);
            }
            else
                Add(obj, ref sErr);
            return sErr;
        }

        public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            return _objDAO.GetTransferOut(dtb, from, to, ref sErr);
        }

        public DataTable ToTransferInStruct()
        {
            return CONGNOInfo.ToDataTable();
        }

        public string TransferIn(DataRow row)
        {
            CONGNOInfo inf = new CONGNOInfo(row);
            return InsertUpdate(inf);
        }
        #endregion Method
        public DataTable GetALLByCT(
        String CT, DateTime NgayCT,
        ref string sErr)
        {
            return _objDAO.GetALLByCT(CT, NgayCT, ref sErr);

        }

        public DataTable GetALLByMaKH(
        String MaKH,
        ref string sErr)
        {
            return _objDAO.GetALLByMaKH(MaKH, ref sErr);
        }

        public DataTable GetALLCongNo(
        String MaKH, String loaiCN, DateTime ngaycn,
        ref string sErr)
        {
            return _objDAO.GetALLCongNo(MaKH, loaiCN, ngaycn, ref sErr);
        }

        public string DeleteCN(string soct, string socttt, DateTime ngayct)
        {
            string sErr = "";
            try
            {
                _objDAO.connect();
                _objDAO.executeNonQuery(string.Format("DELETE FROM CONGNO WHERE SOCT='{0}' and SOCTTT='{1}' and NGAYCT='{2:yyyy-MM-dd}'", soct, socttt, ngayct));
                _objDAO.disconnect();
            }
            catch (Exception ex)
            { sErr = ex.Message; }
            return sErr;

        }
    }
}
